package com.gztms.usermgnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gztms.enums.Gender;
import com.gztms.usermgnt.mapper.RoleMapper;
import com.gztms.usermgnt.mapper.UserMapper;
import com.gztms.usermgnt.pojo.RoleCode;
import com.gztms.usermgnt.pojo.User;
import com.gztms.usermgnt.pojo.vo.UserVO;
import com.gztms.usermgnt.service.AdminService;
import com.gztms.usermgnt.service.UserService;
import com.gztms.utils.BeanCopyUtils;
import com.gztms.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Zoey
 * @date 2023/4/22 20:42
 * @Description TODO
 **/
@Service
public class AdminServiceImpl extends ServiceImpl<UserMapper, User> implements AdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public ResultUtil getAllUser(int pageNum, int pageSize) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        Page<User> userPage = new Page<>(pageNum, pageSize);
        Page<User> users = userService.page(userPage, wrapper);
//        Page<User> users = userMapper.selectPage(userPage,wrapper);
        Map<String,Object> map = new HashMap<>();

        if (Objects.isNull(userPage.getRecords())) {
            return new ResultUtil(ResultUtil.SERVER_ERROR, "当前没有任何用户");
        }else {
            map.put("users",users.getRecords());
            map.put("total", users.getTotal());
            return new ResultUtil(ResultUtil.SUCCESS,"用户查询成功",map);
        }

    }

    @Override
    public ResultUtil getAdminList(String username, String uid, int pageNum, int pageSize) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        Page<User> page = new Page<>(pageNum,-1);

        if (Objects.nonNull(username) && StringUtils.hasText(username)) {
            queryWrapper.eq(User::getUsername,username);
        }
        if (Objects.nonNull(uid) && StringUtils.hasText(uid)) {
            queryWrapper.eq(User::getUid,uid);
        }
        Page<User> userPage = page(page, queryWrapper);
        List<User> users = userPage.getRecords();
        // 封装数据
        List<UserVO> userVOS = BeanCopyUtils.copyBeanList(users, UserVO.class);
        List<UserVO> collects = userVOS.stream()
                .map(userVO -> {
                    // 查询用户角色
                    List<String> code = roleMapper.selectRoleCode(userVO.getUid());
                    userVO.setPermissions(code);
                    if (userVO.getGender().equals(Gender.MALE)) {
                        userVO.setGenderName("男");
                    } else if (userVO.getGender().equals(Gender.FEMALE)) {
                        userVO.setGenderName("女");
                    } else {
                        userVO.setGenderName("未知");
                    }
                    return userVO;
                })
                .collect(Collectors.toList())
                .stream()
                .filter(userVO -> userVO.getPermissions().contains(RoleCode.admin))
                .collect(Collectors.toList());
        List<UserVO> page1 = getPage(collects, pageNum, pageSize);


        Map<String,Object> map = new HashMap<>();
        map.put("admins",page1);
        map.put("total", collects.size());


        return ResultUtil.okResult(map);
    }

    public static <T> List<T> getPage(List<T> list, int pageNum, int pageSize) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        int totalSize = list.size();
        int pageCount = (totalSize + pageSize - 1) / pageSize;
        if (pageNum < 1) {
            pageNum = 1;
        } else if (pageNum > pageCount) {
            pageNum = pageCount;
        }
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, totalSize);
        if (start > end) {
            return Collections.emptyList();
        }
        return list.subList(start, end);
    }
}
